package view;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import transfer.ChatData;
import util.Utils;
import client.Client;
import java.awt.Panel;
import java.awt.Font;
import javax.swing.SwingConstants;

public class ChatForm extends ChessOnlineView {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6737499366438995089L;
	private JTextField textFieldContent;
	private JTextArea textAreaChat;
	private Panel panelColorTeam;
	private JLabel lblTurn;
	private JScrollPane scrollPane;
	private int  idTeam ;
	private final static String[] teamNames = {"Team A","Team B"};
	private String nameOnChat;
	public ChatForm(Client mainScreen,int id) {
	super(mainScreen);
		this.idTeam = id;
		this.setBorder(new TitledBorder(new EtchedBorder(), "Chat Area"));
		this.setPreferredSize(new Dimension(200, 600));
		this.setLayout(null);
		//frame.getContentPane().add(chatPanel,BorderLayout.LINE_END);
		this.nameOnChat = Utils.getCurrentUser(); // set name on chat
		/*lblPlayer = new JLabel(Utils.getCurrentUser()); // set name of current user for label
		lblPlayer.setBounds(10, 279, 62, 14);
		this.add(lblPlayer);*/
//		textAreaChat.setBounds(10, 40, 180, 89);
		
		
		textAreaChat = new JTextArea();
		//textAreaChat.setBounds(10, 99, 161, 103);
		//chatPanel.add(textAreaChat);
		textAreaChat.setForeground(new Color(0, 255, 0));
		textAreaChat.setBackground(Color.BLACK);
		scrollPane = new JScrollPane(textAreaChat);
		scrollPane.setBounds(10, 24, 180, 244);
		scrollPane.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		this.add(scrollPane);
		
		textFieldContent = new JTextField();
		
		textFieldContent.setBounds(10, 276, 180, 45);
		this.add(textFieldContent);
		textFieldContent.setColumns(10);
		
		JButton btnQuitGame = new JButton("Quit Game");
	
		btnQuitGame.setBounds(10, 532, 180, 23);
		add(btnQuitGame);
		
		panelColorTeam = new Panel();
		panelColorTeam.setBackground(Color.WHITE);
		panelColorTeam.setBounds(137, 351, 35, 23);
		add(panelColorTeam);
		
		JLabel lblNewLabel = new JLabel("Your team color :");
		lblNewLabel.setForeground(Color.MAGENTA);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblNewLabel.setBounds(10, 351, 96, 23);
		add(lblNewLabel);
		
		lblTurn = new JLabel("Your Turn");
		lblTurn.setForeground(Color.RED);
		lblTurn.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblTurn.setHorizontalAlignment(SwingConstants.CENTER);
		lblTurn.setBounds(10, 394, 180, 51);
		add(lblTurn);
				// key enter
				textFieldContent.addKeyListener(new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent arg0) {

					}

					@Override
					public void keyReleased(KeyEvent e) {
						if (e.getKeyCode() == KeyEvent.VK_ENTER) {
							sendChatContent();
						}
					}
				});
				if(idTeam == 0){ // team A
					panelColorTeam.setBackground(Color.yellow);
					lblTurn.setText("Your turn");
				}
				else{ // team B
					panelColorTeam.setBackground(Color.blue);
					lblTurn.setText("Opponent turn");
				}
				btnQuitGame.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						int n = JOptionPane.showConfirmDialog(null,"Quitter never win, are you want to quit this game ?", "Quit game", JOptionPane.YES_NO_OPTION);
						if(n == JOptionPane.YES_OPTION){
							mMainScreen.getCw().sendObject(new ChatData("Quit Game",Utils.currentRoomID,teamNames[idTeam]));
						}
					}
				});
	}
	
/*	public JLabel getLblPlayer() {
		return lblPlayer;
	}

	public void setLblPlayer(JLabel lblPlayer) {
		this.lblPlayer = lblPlayer;
	}*/
/*	public void setTeamName(String name){
		lblPlayer.setText(name +" : ");
	}*/
	
	private void sendChatContent() {
		String content = textFieldContent.getText();
		if (!content.equals("")) {
			content = nameOnChat +" : " + content + "\n";
			textAreaChat.append(content);
			textFieldContent.setText("");
			
			mMainScreen.getCw().sendObject(new ChatData("Chat content",Utils.currentRoomID, content));
			
		}
	}
	public void appendChatString(String s){
		textAreaChat.append(s);
	}

/*	public void loadTeam(int id) {
		if(id == 0){ // team A
			panelColorTeam.setBackground(Color.yellow);
			lblTurn.setText("Your turn");
		}
		else{ // team B
			panelColorTeam.setBackground(Color.blue);
			lblTurn.setText("Opponent turn");
		}
			
		
	}*/

	public JLabel getLblTurn() {
		return lblTurn;
	}

	public void setLblTurn(JLabel lblTurn) {
		this.lblTurn = lblTurn;
	}
}
